package com.xkcoding.config;


import com.xkcoding.action.AccountingAction;
import com.xkcoding.action.OrderAction;
import com.xkcoding.action.PayAction;
import com.xkcoding.action.TransferAction;
import com.xkcoding.pipeline.ProcessController;
import com.xkcoding.pipeline.ProcessTemplate;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * handler层的pipeline配置类
 *
 * @author 3y
 */
@Configuration
public class TaskPipelineConfig {
    public static final String PIPELINE_HANDLER_CODE = "handler";

    /**
     * 核算action
     */
    @Resource
    private AccountingAction accountingAction;
    /**
     * 订单action
     */
    @Resource
    private OrderAction orderAction;
    /**
     * 支付action
     */
    @Resource
    private PayAction payAction;

    /**
     * 转账action
     */
    @Resource
    private TransferAction transferAction;

    /**
     * 消息从MQ消费的流程
     * 0.下单
     * 1.支付
     * 2.转账
     * 3.核算
     *
     * @return
     */
    @Bean("taskTemplate")
    public ProcessTemplate taskTemplate() {
        ProcessTemplate processTemplate = new ProcessTemplate();
        processTemplate.setProcessList(Arrays.asList(orderAction, payAction, transferAction, accountingAction));
        return processTemplate;
    }

    /**
     * pipeline流程控制器
     * 后续扩展则加BusinessCode和ProcessTemplate
     *
     * @return
     */
    @Bean("handlerProcessController")
    public ProcessController processController() {
        ProcessController processController = new ProcessController();
        Map<String, ProcessTemplate> templateConfig = new HashMap<>(4);
        templateConfig.put(PIPELINE_HANDLER_CODE, taskTemplate());
        processController.setTemplateConfig(templateConfig);
        return processController;
    }
}
